package com.cloudkinto.controller.product;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.service.product.vo.ProductOutDetailRes;
import com.cloudkinto.service.product.vo.ProductOutExportReq;
import com.cloudkinto.service.product.vo.ProductOutRes;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.product.ProductOutService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloudkinto.base.BaseController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2022-09-21
 */
@RestController
@Api(tags = "出库单导入模块")
@RequestMapping("/api/admin/ProductOut")
public class ProductOutController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ProductOutController.class);

    @Autowired
    private ProductOutService service;


    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit(getCurrentUserId(), getCompanyId());
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功",service.add(req,userId));
    }
    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功",service.update(req,userId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id,userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    public SingleResult list(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        map.put("keyword", keyword);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = ProductOutRes.class))
    @ApiImplicitParams({@ApiImplicitParam(name = "storageId", value = "所在仓库"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "shopId", value = "店铺", required = true),
            @ApiImplicitParam(name = "timeType", value = "周月  week month ", required = true),
            @ApiImplicitParam(name = "sku", value = "sku"),
            @ApiImplicitParam(name = "orderInputNumber", value = "入库单号"),
            @ApiImplicitParam(name = "outNo", value = "出库单号"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "current", value = "当前页"),})
    public PageResult listPage(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");

        String shopId = request.getParameter("shopId");

        String storageId = request.getParameter("storageId");
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        String timeType = request.getParameter("timeType");
        String sku = request.getParameter("sku");
        String orderInputNumber = request.getParameter("orderInputNumber");
        String outNo = request.getParameter("outNo");

        if (StringUtils.isNotBlank(storageId)){
            map.put("storageId", storageId);
        }
        if (StringUtils.isNotBlank(shopId)){
            map.put("shopId", shopId);
        }
        if (StringUtils.isNotBlank(beginTime)){
            map.put("beginTime", beginTime);
        }
        if (StringUtils.isNotBlank(endTime)){
            map.put("endTime", endTime);
        }
        if (StringUtils.isNotBlank(timeType)){
            map.put("timeType", timeType);
        }
        if (StringUtils.isNotBlank(sku)){
            map.put("sku", sku);
        }
        if (StringUtils.isNotBlank(orderInputNumber)){
            map.put("orderInputNumber", orderInputNumber);
        }
        if (StringUtils.isNotBlank(outNo)){
            map.put("outNo", outNo);
        }

        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


    @UserSessionRequired
    @PostMapping("/importOrderOut")
    @ApiOperation(value = "导入Excel")
    public SingleResult importOrderOut(MultipartFile file, @RequestHeader("shopId") Long shopId)  throws IOException{
        return SingleResult.success("导入成功",service.importOrderOut(file, shopId, getCompanyId(),getCurrentUserId()));
    }

    @UserSessionRequired
    @GetMapping("/getOutDetail")
    @ApiOperation(value = "查询出库详情", notes = "查询出库详情")
    @ApiResponses(@ApiResponse(code = 200 , message = "", response = ProductOutDetailRes.class))
    public PageResult getOutDetail(Long shopId, String timeType, String initialDate, String skuEq, String keyword, int current, int pageSize){
        return service.getOutDetail(shopId, timeType, initialDate, skuEq, keyword, current, pageSize);
    }


    @UserSessionRequired
    @GetMapping("/getOutDetailBySku")
    @ApiOperation(value = "查询出库详情Sku维度", notes = "查询出库详情Sku维度")
    @ApiResponses(@ApiResponse(code = 200 , message = "", response = ProductOutDetailRes.class))
    public PageResult getOutDetailBySku(Long shopId, String timeType, String initialDate, String keyword, int current, int pageSize){
        return service.getOutDetailBySku(shopId, timeType, Arrays.asList(initialDate), keyword, current, pageSize);
    }


    @UserSessionRequired
    @PostMapping("exportExcel")
    @ApiOperation(value = "导出数据excel")
    public void exportAll(@RequestBody ProductOutExportReq req, HttpServletResponse response) throws IOException {
        service.export(req, response);
    }



    @UserSessionRequired
    @PostMapping("exportOverView")
    @ApiOperation(value = "导出数据excel")
    public void exportOverView(@RequestBody ProductOutExportReq req, HttpServletResponse response) throws IOException {
        service.exportOverView(req, response);
    }


    @UserSessionRequired
    @PostMapping("exportStockLog")
    @ApiOperation(value = "导出出库记录")
    public void exportStockLog(@RequestBody ProductOutExportReq req, HttpServletResponse response) throws IOException {
        service.exportStockLog(req, response);
    }
}

